package com.itmk.config.web.sys_user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.config.web.sys_user.entity.SysUser;
import com.itmk.config.web.sys_user.entity.UserPageParm;
import com.itmk.config.web.sys_user.service.SysUserService;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    @PostMapping
    public ResultVO add(@RequestBody SysUser sysUser){

        sysUserService.addUser(sysUser);
        return ResultUtils.success("新增成功");
    }

    @PutMapping
    public ResultVO edit(@RequestBody SysUser sysUser){

        sysUserService.addUser(sysUser);
        return ResultUtils.success("编辑成功");
    }

    @DeleteMapping("/{userId}")
    public ResultVO delete(@PathVariable("userId") Long userId){

        sysUserService.deleteUser(userId);
        return ResultUtils.success("删除成功");
    }

    @GetMapping("/list")
    public ResultVO list(UserPageParm parm){
        //构造分页对象
        IPage<SysUser> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(parm.getNickName())){
            query.lambda().like(SysUser::getNickName,parm.getNickName());
        }
        if (StringUtils.isNotEmpty(parm.getPhone())){
            query.lambda().like(SysUser::getPhone,parm.getPhone());
        }
        query.lambda().orderByDesc(SysUser::getCreateTime);
        IPage<SysUser> list = sysUserService.page(page,query);
        return ResultUtils.success("查询成功",list);
    }
}
